package main

import (
	"fmt"
	"regexp"
	"strconv"
)

// 使用regexp包进行匹配, 替换和查找

func main() {
	// 在字符串查找所有数字
	s := "John: 2578.34 William: 4567.23 Steve: 5632.18"
	pat := "[0-9]+.[0-9]+"

	if ok, _ := regexp.Match(pat, []byte(s)); ok {
		fmt.Println(s, " Mathed the pattern: ", pat)
	}
	c, err := regexp.Compile(pat)
	if err == nil {
		secret := c.ReplaceAllString(s, "**.**")
		fmt.Println(secret)

		// 字符串中数字放大2倍
		f := func(s string) string {
			d, _ := strconv.ParseFloat(s, 64)
			return strconv.FormatFloat(d*2, 'f', 2, 64)
		}
		s = c.ReplaceAllStringFunc(s, f)
		fmt.Println(s)
	}
}
